package cn.itcast.util;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

/**
 * 七牛文件处理工具类
 *
 * @author wangjunfeng
 */
public class QiniuUtils {
    public static String accessKey = "BviGR0OB8KJ4ImYg1jULBa1GmxfVcO31i_drNzo2";
    public static String secretKey = "9PSP5cY6xu-hbWv42bXbfKyWIBJkv1aEx4XpyQ7p";
    public static String bucket = "lylleiylong2";
    /**
     * Region 必须和自己在创建空间时选择的存储区域一致
     * <p>
     * 机房	Region
     * 华东	Region.region0(), Region.huadong()<p>
     * 华北	Region.region1(), Region.huabei()<p>
     * 华南	Region.region2(), Region.huanan()<p>
     * 北美	Region.regionNa0(), Region.beimei()<p>
     * 东南亚	Region.regionAs0(), Region.xinjiapo()
     */
    private static Configuration cfg = new Configuration(Region.region2());

    private static UploadManager uploadManager = new UploadManager(cfg);

    private static Auth auth = Auth.create(accessKey, secretKey);

    private static String upToken = auth.uploadToken(bucket);

    /**
     * 上传文件到七牛服务器
     *
     * @param filePath 文件路径
     * @param fileName 存储到七牛服务器上的文件名
     */
    public static void upload(String filePath, String fileName) {
        try {
            Response response = uploadManager.put(filePath, fileName, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {

            }
        }
    }

    /**
     * 上传文件到七牛服务器
     *
     * @param bytes    文件的字节数组
     * @param fileName 存储到七牛服务器上的文件名
     */
    public static void upload(byte[] bytes, String fileName) {
        try {
            Response response = uploadManager.put(bytes, fileName, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                System.err.println(ex2.code());
                System.err.println(ex2.response.toString());
            }
        }
    }

    /**
     * 删除七牛服务器上的文件
     *
     * @param fileName 要删除的文件名
     */
    public static void delete(String fileName) {
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(bucket, fileName);
        } catch (QiniuException ex) {
            System.err.println(ex.code());
            System.err.println(ex.response.toString());
        }
    }
}
